package com.longtop.perform.khyg.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.StringUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.perform.khyg.model.TKhygSegmentMx;
import com.longtop.perform.khyg.service.ProjectService;

/**
 * 
 * @author 孙景弘
 * 
 */
public class ProjectSegmentAction extends BaseAction {

	private ProjectService service;
	private final String DATASET_PROJECT_SEGMENT = "Dataset_project_segment";
	private final String DATASET_PROJECT_SEGMENT_USER = "Dataset_project_segment_user";
	private final String DATASET_USER = "Dataset_user";

	// 查询条件：
	Map<String, Object> queryMap = new HashMap<String, Object>();

	public ProjectSegmentAction() {
		this.service = this.getBean(ProjectService.BEAN_ID);
	}

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		Dataset dataset_project_segment = this.getDataset(context, DATASET_PROJECT_SEGMENT);
		if (!WebUtil.validateDatasetExist(this, dataset_project_segment, DATASET_PROJECT_SEGMENT))
			return;

		String projectId = this.getParameter(context, "projectId");
		queryMap.put("projectId", projectId);
		if (!StringUtil.isEmpty(projectId)) {
			// 获取考核方案对应分段
			List list = service.getProjectSegment(projectId);
			BeanUtil.listToDataset(list, dataset_project_segment, false);
		}
	}

	/**
	 * 获取考核阶段对应的员工
	 * 
	 * @param context
	 */
	public void queryProjectSegmentUser(IActionContext context) {
		Dataset dataset_project_segement_user = this.getDataset(context, DATASET_PROJECT_SEGMENT_USER);
		if (!WebUtil.validateDatasetExist(this, dataset_project_segement_user, DATASET_PROJECT_SEGMENT_USER))
			return;

		String segmentId = this.getParameter(context, "segmentId");
		queryMap.put("segmentId", segmentId);
		service.queryForPageByIbatis("khygProjectSql.queryProjectSegmentUser", null, queryMap, dataset_project_segement_user, false);
	}

	/**
	 * 根据查询条件查找员工
	 * 
	 * @param context
	 */
	public void queryUsers(IActionContext context) {
		Dataset dataset_user = this.getDataset(context, DATASET_USER);
		if (!WebUtil.validateDatasetExist(this, dataset_user, DATASET_USER))
			return;

		queryMap = this.getElementsToMap(context);// 获取自定义查询条件
		service.queryForPageByIbatis("khygProjectSql.queryUserList", null, queryMap, dataset_user, false);
	}

	/**
	 * 提交保存考核阶段对应员工
	 * 
	 * @param context
	 */
	public void submitSegmentUser(IActionContext context) {
		Dataset dataset_project_segement_user = this.getDataset(context, DATASET_PROJECT_SEGMENT_USER);
		if (!WebUtil.validateDatasetChange(this, dataset_project_segement_user, DATASET_PROJECT_SEGMENT_USER))
			return;
		try {
			// 方案对应指标：
			String projectId = this.getParameter(context, "projectId");
			queryMap.put("projectId", projectId);
			List indexList = service.queryForListByIbatis("khygProjectSql.projectIndexIdToName", queryMap);

			String segmentId = this.getParameter(context, "segmentId");
			String segmentName = this.getParameter(context, "segmentName");
			Map changeMap = WebUtil.datasetToObjectList(dataset_project_segement_user, "id", TKhygSegmentMx.class, "orgName");
			SessionObject sessionObject = this.getSessionObject(context);

			Integer[] count = service.saveSegmentUser(changeMap, segmentId, segmentName, indexList, sessionObject);

			String msg = WebUtil.getSubmitSuccessInfo(count[0].intValue(), count[1].intValue(), count[2].intValue());

			this.hintInfoMessage(msg);
		} catch (Exception e) {
			this.hintErrorMessage("保存考核对象出错。");
			e.printStackTrace();
		}
	}

	/**
	 * 全部删除考核阶段对应员工
	 * 
	 * @param context
	 */
	public void deleteAllSegmentUser(IActionContext context) {
		String segmentId = this.getParameter(context, "segmentId");
		try {

			service.deleteAllSegmentUser(segmentId);
			this.hintInfoMessage("全部删除成功。");
		} catch (Exception e) {
			this.hintErrorMessage("全部删除考核对象出错。");
			e.printStackTrace();
		}
	}
}
